package cn.zhangchg.leetcode.object;

import org.openjdk.jol.info.ClassLayout;

/**
 * 重量级锁
 */
public class SeeBin {
    static Person person = new Person();
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(5000);
        //三个线程去竞争访问一个锁对象
        for (int i = 0; i < 3; i++) {
            int finalI = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(finalI + ClassLayout.parseInstance(person).toPrintable());
                    synchronized (person){
                        System.out.println(finalI +ClassLayout.parseInstance(person).toPrintable());
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(finalI +ClassLayout.parseInstance(person).toPrintable());
                    }
                    System.out.println(finalI +ClassLayout.parseInstance(person).toPrintable());
                }
            }).start();
        }
    }
}